
var SynoLoader = {};
Components.utils.import("resource://SynoLoader/DSM3.js",SynoLoader);  




(function() {

  this.prefs=null;
  this.onLoad= function() {
    var mediator = Components.classes['@mozilla.org/appshell/window-mediator;1']
                  .getService(Components.interfaces.nsIWindowMediator);
    var doc = mediator.getMostRecentWindow("navigator:browser").document;
    if(SynoLoader.SynoLoaderDMS.syno_download_station.initialized==false)
    {
    // initialization code


    this.strings = doc.getElementById("SynoLoader-strings");

    this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
				.getService(Components.interfaces.nsIPrefService)
				.getBranch("extensions.SynoLoader.");
    
    this.prefs = Components.classes["@mozilla.org/preferences-service;1"]
	.getService(Components.interfaces.nsIPrefService)
	.getBranch("extensions.SynoLoader.");
    
    this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
    this.prefs.addObserver("", this, false);

    var myLoginManager = Components.classes["@mozilla.org/login-manager;1"].
		                  getService(Components.interfaces.nsILoginManager);
		  
	// Find users for the given parameters
	var logins = myLoginManager.findLogins({}, 'chrome://SynoLoader.Pass', null, 'User Registration');
	if( logins.length > 0) {
		this.SynoLoaderDMS.DMS_Connect.username = logins[0].username;
		this.SynoLoaderDMS.DMS_Connect.password = logins[0].password;
	}

   


   this.SynoLoaderDMS.syno_download_station.window=window;
   this.SynoLoaderDMS.Notification.start_dlm=this.prefs.getBoolPref('start_dlm');
   this.SynoLoaderDMS.Notification.show_not=this.prefs.getBoolPref('show_not');
   this.SynoLoaderDMS.Util.show_log=this.prefs.getBoolPref('show_dgb');
   this.SynoLoaderDMS.syno_download_station.url=this.prefs.getCharPref('url');
   this.SynoLoaderDMS.syno_download_station.emule_downloadfolder=this.prefs.getCharPref('emule_downloadfolder');
   this.SynoLoaderDMS.syno_download_station.DSMVerison=this.prefs.getCharPref('DSM_Verison');
   this.SynoLoaderDMS.syno_download_station.connect_to_nas("");
   this.interval=window.setInterval(function(){SynoLoader.SynoLoaderDMS.syno_download_station.GetAll();},5000);
   this.SynoLoaderDMS.syno_download_station.initialized = true;
   
   }
  };


  this.observe = function(subject, topic, data)
  {
	  if (topic == "nsPref:changed")
	  {
		  switch(data)
		  {
		  case 'start_dlm':
			  this.SynoLoaderDMS.Notification.start_dlm=this.prefs.getBoolPref('start_dlm');
			  break;
		  case 'url':
			  this.SynoLoaderDMS.syno_download_station.url=this.prefs.getCharPref('url');
			  break;
		  case 'show_not':
	          this.SynoLoaderDMS.Notification.show_not=this.prefs.getBoolPref('show_not');
	          break;
		  case 'show_dgb':
			  this.SynoLoaderDMS.Util.show_log=this.prefs.getBoolPref('show_dgb');
			  break;
		  case 'DSM_Verison':
			  this.SynoLoaderDMS.syno_download_station.DSMVerison=this.prefs.getCharPref('DSM_Verison');
			  break;
		  case 'emule_downloadfolder':
			  this.SynoLoaderDMS.syno_download_station.emule_downloadfolder=this.prefs.getBoolPref('emule_downloadfolder');
			  break;

		  }       
	  } 
  };

  this.shutdown= function()
  {

	window.removeEventListener("load", function (e) { SynoLoader.onFirefoxLoad(); }, false);
	window.removeEventListener("load", function (e) { SynoLoader.onLoad(); }, false);
	window.removeEventListener("unload", function(e) { SynoLoader.shutdown(); }, false);
	window.clearInterval(this.intvall);
  	document.getElementById("contentAreaContextMenu")
        .removeEventListener("popupshowing", function (e){ SynoLoader.showFirefoxContextMenu(e); }, false);
  };

 

  this.onToolbarButtonCommand= function(e) {
    // just reuse the function above.  you can change this, obviously!
    this.onMenuItemCommand(e);
  };

 
}).apply(SynoLoader);


